QuickStartWindow = {}

QuickStartWindow.iQueueTime = 0
QuickStartWindow.iQueuePos = 0
QuickStartWindow.iQueueSize = 0
QuickStartWindow.iLastUpdate = 0

QuickStartWindow.iMaxQueueTime = 65535 -- the max server time (0xFFFF) is sent down when the server doesn't know what time to send so we won't display a time at that time

QuickStartWindow.trialPlayer = false
QuickStartWindow.buddiedPlayer = false

QuickStartWindow.noTransferMessageServers = {L"Jason", L"Int Test [1.2.0]", L"Phoenix Throne"}

function QuickStartWindow.Initialize()

    WindowRegisterEventHandler( "QuickStartWindow", SystemData.Events.CHARACTER_QUEUE_UPDATED, "QuickStartWindow.OnQueueUpdated")

    ButtonSetText( "QuickStartWindowCinematicButton",  GetPregameString( StringTables.Pregame.LABEL_BUTTON_PRELOGIN_CINEMATIC  )  )
    ButtonSetText( "QuickStartWindowCreditsButton",    GetPregameString( StringTables.Pregame.LABEL_BUTTON_PRELOGIN_CREDITS    )  )
    ButtonSetText( "QuickStartWindowSettingsButton",   GetPregameString( StringTables.Pregame.LABEL_BUTTON_PRELOGIN_SETTINGS   )  )
    ButtonSetText( "QuickStartWindowQuitButton",       GetPregameString( StringTables.Pregame.LABEL_QUIT                       )  )
    ButtonSetText("QuickStartWindowPlay",              GetPregameString( StringTables.Pregame.LABEL_PLAY                       )  )
    ButtonSetText("QuickStartWindowCharactersButton",  GetPregameString( StringTables.Pregame.LABEL_BUTTON_PRELOGIN_CHARACTERS )  )
    ButtonSetText("QuickStartWindowServersButton",     GetPregameString( StringTables.Pregame.LABEL_BUTTON_PRELOGIN_SERVERS    )  )
    ButtonSetText( "QuickStartWindowUpgradeTrial",     GetPregameString( StringTables.Pregame.LABEL_UPGRADE                    )  )

    QuickStartWindow.RefreshPlayerData()
    if QuickStartWindow.trialPlayer
    then
        WindowSetShowing("QuickStartWindowUpgradeTrial", true)
    else
        WindowSetShowing("QuickStartWindowUpgradeTrial", false)
    end

    local nameLabel, rank, careerLabel, locationLabel = PregameGetQuickStartLabels( )
    LabelSetText("QuickStartWindowNameLabel", nameLabel)
	local rankAndCareerLineText = GetPregameStringFormat( StringTables.Pregame.TEXT_QUICK_START_LINE_TWO, { rank, careerLabel} )
    LabelSetText("QuickStartWindowRankLabel", rankAndCareerLineText)
    LabelSetText("QuickStartWindowLocationLabel", locationLabel)
    
    -- disable the cinematic button if their audio driver is not supported
    if CanPlayCinematic() == true
    then
        ButtonSetDisabledFlag( "QuickStartWindowCinematicButton", false )
    else
        ButtonSetDisabledFlag( "QuickStartWindowCinematicButton", true )
    end

    WindowSetShowing("QuickStartQueueStatusWindow", false)

    if (DoesWindowExist("LoginProgressWindow")) then
        WindowSetShowing("LoginProgressWindow", false)
    end

    -- update the server name label if the LobbyBackground window exists
    if (DoesWindowExist("LobbyBackground")) then
        LobbyBackground.UpdateServerName()
    end

    -- if the transfer flag hasn't been used yet
    if not SystemData.Server.TransferFlagUsed
    then
        -- set the use flag
        SystemData.Server.TransferFlagUsed = true

        local allowTransferMessage = true

        for _, name in ipairs(QuickStartWindow.noTransferMessageServers)
        do
            if (name == SystemData.Server.Name)
            then
                allowTransferMessage = false
                break
            end
        end
        
        -- if the transfer flag is on
        if( (SystemData.Server.TransferFlag == 1) and allowTransferMessage )
        then
        
            local serverStatus = EA_Window_TransferPopup.OPEN_SERVER
            local serverInfo = InterfaceCore.GetServerInfo( SystemData.Server.ID )
            if( serverInfo and serverInfo.legacy)
            then
                serverStatus = EA_Window_TransferPopup.LEGACY_SERVER
            end
            
            -- just in case the window doesn't exist
            if (DoesWindowExist("EA_Window_Transfer")) then
                EA_Window_TransferPopup.UpdateServerText( serverStatus )
                EA_Window_TransferPopup.Show()
            end
        end
    end
end

function QuickStartWindow.Update(timeElapsed)
end

function QuickStartWindow.Shutdown()
end

function QuickStartWindow.QuitGame()
    BroadcastEvent( SystemData.Events.QUIT )
end

function QuickStartWindow.ToggleSettings()
    local showing = WindowGetShowing("SettingsWindowTabbed")
    WindowSetShowing("SettingsWindowTabbed", not showing)
end

-- OnLButtonUp Handler for the 'Play' Button
function QuickStartWindow.Play( flags, mouseX, mouseY )

    BroadcastEvent( SystemData.Events.PLAY)

end

-- OnLButtonUp Handler for the 'Servers' Button
function QuickStartWindow.GoToServers( flags, mouseX, mouseY )

    BroadcastEvent( SystemData.Events.PREGAME_LAUNCH_SERVER_SELECT )

end

-- OnLButtonUp Handler for the 'Characters' Button
function QuickStartWindow.GoToCharacters( flags, mouseX, mouseY )

    BroadcastEvent( SystemData.Events.PREGAME_GO_TO_CHARACTER_SELECT)

end

function QuickStartWindow.OnQueueUpdated( waitSeconds, queuePos, queueSize )

    WindowSetShowing("QuickStartQueueStatusWindow", true)

    QuickStartWindow.iQueueTime = waitSeconds
    QuickStartWindow.iQueuePos = queuePos
    QuickStartWindow.iQueueSize = queueSize
    QuickStartWindow.iLastUpdate = 0

    QuickStartWindow.UpdateQueueStatus()
end

function QuickStartWindow.UpdateQueueStatus()

    local text = GetPregameStringFormat( StringTables.Pregame.TEXT_REALM_FULL_QUEUE, { QuickStartWindow.iQueuePos, QuickStartWindow.iQueueSize, QuickStartWindow.GetTimeRemainingString() } )
    
    LabelSetText("QuickStartQueueStatusWindowText", text)
    
    if( QuickStartWindow.iQueueTime == 0 )
    then
        QuickStartWindow.ResetPlayButton() 
    else
        -- Disable the play button until the player selects annother character
        ButtonSetDisabledFlag("QuickStartWindowPlay", false )
    end

end

function QuickStartWindow.ResetPlayButton()

    ButtonSetPressedFlag("QuickStartWindowPlay", false )
    ButtonSetDisabledFlag("QuickStartWindowPlay", false )

end

function QuickStartWindow.GetTimeRemainingString()
    if QuickStartWindow.iQueueTime < 60
    then
        return GetPregameString( StringTables.Pregame.LABEL_SMALL_TIMER  )
    elseif QuickStartWindow.iQueueTime == QuickStartWindow.iMaxQueueTime
    then
        return L""
    end

    return TimeUtils.FormatTime(QuickStartWindow.iQueueTime)
end

function QuickStartWindow.RefreshPlayerData()
    QuickStartWindow.trialPlayer, QuickStartWindow.buddiedPlayer = GetAccountData()
end

function QuickStartWindow.UpgradeTrial()
    EA_TrialAlertWindow.OnUpgradeWithOutClose()
end
